package cn.gaozhong.ymcc.exception;

import cn.gaozhong.ymcc.constants.GlobalError;
import cn.gaozhong.ymcc.result.JSONResult;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 全局统一异常处理类
 */
//@ControllerAdvice
@RestControllerAdvice   // @ControllerAdvice   + @ResponseBody
public class GlobalExceptionHandler {

    @ExceptionHandler(value = Exception.class)
    public JSONResult exceptionHandler(Exception e) {
        // 把错误信息输入到日志中
        e.printStackTrace();
        return JSONResult.error(
                GlobalError.ERROR_EXCEPTION.getMessage(),
                GlobalError.ERROR_EXCEPTION.getCode());
    }


    @ExceptionHandler(value = GlobalException.class)
    public JSONResult globalExceptionHandler(GlobalException e) {
        // 把错误信息输入到日志中
        e.printStackTrace();
        return JSONResult.error(e.getMessage(),e.getCode());
    }
}
